<?php
/**
 * @package contenite/samples
 */
class user_controller extends app_controller {
  function index() {
    $user = $this->args[0];
    
    $lob_generator = load('lob');
    list($this->lobe, $this->pager) = $lob_generator->page(array(
      'name' => 'seite',
      'where' => 'user_id = %d',
      'args' => array($user->id),
      'fields' => array('id', 'body', 'user', 'created'),
      'order' => 'created DESC'));
  }

  /**
   * Action
   */
  function contact() {
    $this->title = 'Kontaktaufnahme';
    if ($this->args && $this->args[0]) {
      $this->data = $user = $this->args[0];
      $this->page_title = 'Kontakt zu ' . $user->name;
    }
    else {
      $user_generator = load('user');
      $this->data = $user_generator->read('dine');
       $this->page_title = 'Kontakt zum Site-Betreiber';
    }

    if ($_POST) {
      $calc = $_SESSION['calc'];
      $chk = intval($_POST['chk']);
      // echo  'Aufgabe: ' . $calc['a'] . $op[$calc['n']] . $calc['b'] . ' = ' . $chk . "\n";
      // var_dump($_POST, $text);
      unset($_SESSION['message']);
      switch ($calc['n']) {
        case 0:
          if ($chk != $calc['a'] + $calc['b']) {
            message('<span class="warn">Bitte lösen Sie die Rechenaufgabe</span>'); 
          }
          break;

        case 1:
          if ($chk != $calc['a'] - $calc['b']) {
            message('<span class="warn">Bitte lösen Sie die Rechenaufgabe</span>'); 
          }
          break;

        case 2:
          if ($chk != $calc['a'] * $calc['b']) {
            message('<span class="warn">Bitte lösen Sie die Rechenaufgabe</span>'); 
          }
          break;
      }

      if (!isset($_SESSION['message'])) {
        $text = nl2br($_POST['text']);
        send_mail($user->email, '[d-i-n-e.de] Kontaktanfrage', "Hallo,

Sie haben folgende Kontaktanfrage erhalten:
----
{$text}
----

Viel Spass beim Loben!
Dr. Olav Schettler");

        message('<span class="success">Vielen Dank. Ihre Nachricht wurde weitergeleitet.</span>');
        redirect($user);
      }
    }

    // prep form
    $op = array('plus', 'minus', 'mal');
    $a = rand(1, 10);
    $b = rand(1, 10);

    // make sure minus yields a positive result
    if ($a > $b) {
      $calc['a'] = $a;
      $calc['b'] = $b;
    }
    else {
      $calc['a'] = $b;
      $calc['b'] = $a;
    }
    $calc['n'] = rand(0, 2);
    $this->calc = $calc + array('op' => $op[$calc['n']]);

    $this->text = isset($_POST['text']) ? $_POST['text'] : '';
    $_SESSION['calc'] = $calc;
  }

  /**
   * Action
   */
  function login() {
    $user_generator = load('user');
    if ($_POST) {
      $_SESSION['user'] = $user_generator->one(
        'email = %s AND password = PASSWORD(%s)', 
        $_POST['email'], $_POST['password']);
      $msg = __('Welcome back.');
    }
    else
    if (count($this->path) > 2) {
      $_SESSION['user'] = $user_generator->one('code = %s', $this->path[2]);
      $msg = __('You are logged in. Please change your password.');
    }

    if ($_SESSION['user']) {
      $user = $user_generator->model();
      $user->save(array('code' => NULL, 'last' => time()));
      if (!isset($_SESSION['message'])) {
        message('<span class="success">' . $msg . '</span>');
      }
<<<<<<< .mine
      redirect();
=======
      redir();
>>>>>>> .r67
    }
    else {
      message('<span class="error">' . __('Login failed.') . '</span>');
<<<<<<< .mine
      redirect('#account');
=======
      redir('#account');
>>>>>>> .r67
    }
  }

  /**
   * Action
   */
  function logout() {
    unset($_SESSION['user']);
<<<<<<< .mine
    redirect('/');
=======
    redir('/');
>>>>>>> .r67
  }

  /**
   * Action
   */
  function password() {
    $user_generator = load('user');
    if (!isset($_POST['email'])) {
      message('<span class="error">' . __("We couldn't find your e-mail address.") . '</span>');
<<<<<<< .mine
      redirect('/#account');
=======
      redir('/#account');
>>>>>>> .r67
    }
    $user = $user_generator->one('email = %s', $_POST['email']);
    if ($user) {
      $i = 0;
      do {
        $i++;
        $code = base_convert(rand(), 10, 36);
        if ($user->save(array('code' => $code))) {
          send_mail($_POST['email'], __('Password lost?'), "Hallo,

über den folgenden Link können Sie sich anmelden:

http://{$_SERVER['HTTP_HOST']}/user/login/{$code}

Viel Spass beim Loben!
Dr. Olav Schettler");
          message('<span class="success">' . __('We have just sent you an e-mail.') . '</span>');
          break;
        }
      }
      while ($i < 100);
    }
    else {
      message('<span class="error">' . __("We couldn't find your e-mail address") . '</span>');
    }
<<<<<<< .mine
    redirect('#account');
=======
    redir('#account');
>>>>>>> .r67
  }

  /**
   * Action
   */
  function register() {
    if ($_POST) {
      if ($_POST['email2']) {
        error_log("REGISTER: Break-in attempt " . serialize($_POST));
<<<<<<< .mine
        redirect();
=======
        redir();
>>>>>>> .r67
      }
      
      $user_generator = load('user');

      foreach (array(
        'email' => __('E-Mail'), 
        'password' => __('Password'), 
        'password2' => __('Password (repeated)'), 
        'name' => 'Name') as $field => $title) {
        if (!$_POST[$field]) {
          message('<span class="warn">' 
            . __('Please fill in field %field.', array('%field' => $title)) . '</span>');
        }
      }
      if ($_POST['email'] && !preg_match('#^[-+_\.\w]+@\w[-_\.\w]+$#', $_POST['email'])) {
        message('<span class="warn">' . __('Please use a valid e-mail address') . '</span>');
      }
      if ($_POST['password'] && $_POST['password'] != $_POST['password2']) {
        message('<span class="warn">' . __('The passwords do not match.') . '</span>');
      }
      if ($_POST['name'] && !preg_match('/^[ \w]+$/', $_POST['name'])) {
        message('<span class="warn">' . __('The name may contain only characters and digits') . '</span>');
      }
      if ($_POST['email'] && $user_generator->one('email = %s', $_POST['email'])) {
        message('<span class="warn">' . __('This e-mail address is already registered') . '</span>');
      }

      if (isset($_SESSION['message'])) {
<<<<<<< .mine
        redirect('#register');
=======
        redir('#register');
>>>>>>> .r67
      }

      $user = new user;
      if (($user_id = $user->save($_POST)) !== FALSE) {
        message('<span class="success">' . __('Your registration was successful') . '</span>');
        send_mail($_POST['email'], __("Welcome"), __("Welcome to %server.\nHave fun.", array('%server' => $_SERVER['HTTP_HOST'])));
        $this->login();
      }
      else {
        message('<span class="error">' . __('Registration failed') . '</span>');
<<<<<<< .mine
        redirect('#register');
=======
        redir('#register');
>>>>>>> .r67
      }
    }
  }
}
